<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<!-- <el-form-item>
					<el-input v-model="filters.name" placeholder="设备IP"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary">查询</el-button>
				</el-form-item> -->
				<el-form-item v-show="my_permi.indexOf('role.store') > -1 || my_permi.indexOf('admin') > -1">
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="tableList" highlight-current-row v-loading="listLoading" style="width: 100%;" :height="clientHeight" >
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="角色名">
			</el-table-column>
			<el-table-column prop="created_at" label="创建时间">
			</el-table-column>
			<el-table-column label="操作" width="210">
				<template slot-scope="scope">
					<el-button v-show="my_permi.indexOf('role.show') > -1 || my_permi.indexOf('admin') > -1" size="small" type="primary" @click="router({path: '/logistics/role/roles', query: {id: scope.row.id}})">详情</el-button>
					<el-button v-show="my_permi.indexOf('role.put') > -1 || my_permi.indexOf('admin') > -1" type="warning" size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button v-show="my_permi.indexOf('role.delete') > -1 || my_permi.indexOf('admin') > -1" type="danger" size="small" @click="del(scope.row.id)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-pagination
				layout="prev, pager, next"
				@current-change="handleCurrentChange"
				:total="total"
				style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增界面-->
		<el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="角色名">
					<el-input v-model="addForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="角色权限">
					<el-select
			    	v-model="addForm.permissionIds"
				    multiple
				    collapse-tags
				    placeholder="请选择">
				    <el-option
				      v-for="item in options"
				      :key="item.value"
				      :label="item.label"
				      :value="item.value">
				    </el-option>
				  </el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--编辑界面-->
		<el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" ref="editForm">
				<el-form-item label="角色名">
					<el-input v-model="editForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="角色权限">
					<el-select
			    	v-model="editForm.permissionIds"
				    multiple
				    collapse-tags
				    placeholder="请选择">
				    <el-option
				      v-for="item in options"
				      :key="item.value"
				      :label="item.label"
				      :value="item.value">
				    </el-option>
				  </el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import { roles, role, permission } from '@/api/api';
    import { mapState } from 'vuex'
	export default {
		data() {
			return {
				filters: {
					name: ''
				},
				tableList: [],
				total: 0,
				page: 1,
				listLoading: false,
				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
					name: [
						{ required: true, message: '请输入用户名', trigger: 'blur' }
					]
				},
				//新增界面数据
				addForm: {
					name: '',
					permissionIds: [],
				},
				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				//编辑界面数据
				editForm: {
					name: '',
					permissionIds: [],
				},
				options: [],
			}
		},
        computed: {

            ...mapState(
                {
                  'my_permi': state => state.my_permi,
                  'clientHeight': state => state.clientHeight
                }
            )
        },
		methods: {
			router(path) {
				this.$router.push(path)
			},
			handleCurrentChange(val) {
				this.page = val;
				this.roleList();
			},
			//获取设备警报信息
			roleList() {
				this.listLoading = true;
				role(this.page).then((res) => {
					if(res.data.status.errCode == '10002'){
						sessionStorage.removeItem('token');
						this.$message({
							message: res.data.status.message,
							type: 'warning'
				        });
				        setTimeout(() =>{
				        	this.router({path: '/login'})
				        }, 1500);
						return;
					}
					this.total = res.data.data.total
					this.tableList = res.data.data.data
					this.listLoading = false
				});
			},
			del(id) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					roles(id, 'DELETE').then((res) => {
						this.listLoading = false;
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.roleList();
					});
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				this.editId = row.id;
				this.editPermissionIds = [];
				//获取设备警报信息
				roles(this.editId, 'GET').then((res) => {
					if(res.data.status.errCode == '0'){
						for(var i in res.data.data.permissions){
							this.editPermissionIds.push(res.data.data.permissions[i].id)
						}
						this.editForm = {
							name: row.name,
							permissionIds: this.editPermissionIds,
						};
					}
				});
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							let para = Object.assign({}, this.editForm);
							roles(this.editId, 'PUT', para).then((res) => {
								if(res.data.status.errCode != '0'){
									this.$message({
										message: res.data.status.message,
										type: 'error'
									});
								}else{
									this.$message({
										message: res.data.status.message,
										type: 'success'
									});
								}
								this.editLoading = false;
								this.$refs['editForm'].resetFields();
								this.editFormVisible = false;
								this.roleList();
							}).catch((err) => {
								console.dir(err)
							});
						});
					}
				});
			},
			handleAdd() {
				this.addFormVisible = true;
				this.addForm = {
					name: '',
					permissionIds: [],
				};
			},
			//新增
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.addForm);
							roles('', 'POST', para).then((res) => {
								if(res.data.status.errCode != '0'){
									this.$message({
										message: res.data.status.message,
										type: 'error'
									});
								}else{
									this.$message({
										message: res.data.status.message,
										type: 'success'
									});
								}
								this.addLoading = false;
								this.$refs['addForm'].resetFields();
								this.addFormVisible = false;
								this.roleList();
							});
						});
					}
				});
			},
			// 获取权限列表
			permissionList() {
				permission().then((res) => {
					this.options = [];
					for(var i in res.data.data){
						var arr = {
							label: res.data.data[i].desc,
							value: res.data.data[i].id,
						}
						this.options.push(arr)
					}
				})
			}
		},
		mounted() {
			this.roleList();
			this.permissionList()
		}
	}

</script>

<style scoped>

</style>